$(document).ready(function () {

    $("#selectThesisDo").click(function () {
        $.confirm({
            title: '验证',
            confirmButton: '确定选择',
            cancelButton: '取消',
            confirmButtonClass: 'btn-success',
            cancelButtonClass: 'btn-danger',
            autoClose: 'cancel|30000',
            content: "请输入该题目的授权码<br><input id='key' class='form-control'>",
            confirm: function () {
                //需要6位授权码
                key=$("#key").val();
                if (key.length != 6) {
                    $.alert({
                        confirmButton: '知道了',
                        confirmButtonClass: 'btn-danger',
                        autoClose: 'confirm|5000',
                        title: false,
                        content: "请输入6位授权码"
                    });
                } else {
                    select(key);
                }
            }
        });
    });

    function select(key) {
        //选择题目
        $.post("/s/selectThesis", {
            tisId: $("#tisId").text(),
            tisKey: key
        })
        //请求成功
        .success(function (result) {
            if (result === 1) {
                $.alert({
                    confirmButton: '知道了',
                    confirmButtonClass: 'btn-success',
                    autoClose: 'confirm|5000',
                    title: false,
                    content: "选择成功",
                    confirm: function () {
                        showList();
                    }
                });
            } else {
                $.alert({
                    confirmButton: '知道了',
                    confirmButtonClass: 'btn-danger',
                    autoClose: 'confirm|5000',
                    title: false,
                    content: result,
                    confirm:function () {
                        showList();
                    }
                });
            }
        })
        //请求失败
        .error(function () {
            $.alert({
                confirmButton: '知道了',
                confirmButtonClass: 'btn-danger',
                autoClose: 'confirm|5000',
                title: false,
                content: "加载失败，请稍后再试"
            });
        });
    }

    //添加成功后，跳转至审核列表
    function showList() {
        $.post("/s/thesisList")
            .success(function (data) {
                //判断返回的是否为HTML
                if (data.indexOf("<head>") != -1 && data.indexOf("</head>") != -1 && data.indexOf("</html>"))
                    $("#content").html(data);
                else {
                    $.alert({
                        confirmButton: '知道了',
                        confirmButtonClass: 'btn-danger',
                        autoClose: 'confirm|10000',
                        title: false,
                        content: data,
                        confirm: function () {
                            window.location.replace('');
                        }
                    });
                }
            })
            .error(function () {
                $.alert({
                    confirmButton: '知道了',
                    confirmButtonClass: 'btn-danger',
                    autoClose: 'confirm|5000',
                    title: false,
                    content: "加载列表失败，请稍后再试"
                });
            });
    }
});